Barakus_Core_ActionController = Class.extend({
  init: function(request)
  {
    this.request = request;
  },
  doAction: function()
  {
    var page = strtolower(this.request.get.getParam('page'));
    var action = null;
    if (empty(page))
    {
		page = 'initial';
    } else {
        action = this.request.get.getParam('action');
    }

    var controllerFile = 'controller/' + page + '.js';
	var className = 'App_' + ucfirst(page) + 'Controller';
    try {
        BARAKUS.webInclude(controllerFile, true);
    } catch (err) {
		opera.postError(controllerFile + ' not found! ' + err.toString());
    }        
	var controller = null;
    if (class_exists(className))
    {
		var controller = null;
		eval('controller = new ' + className + '()');
		
		controller.preLoad(page);
		controller.onLoad();
		
		if (!empty(action))
		{
			var formattedAction = action;
			if (this.request.isPost())
			{
				formattedAction = 'post_' + action;
			} else if (action.match(/^post_/i)){
				throw new Error('Invalid Action!');
			}
			if (method_exists(controller, formattedAction))
			{
				controller.view.load(page + '/' + action + '.tpl');
				eval('controller.' + formattedAction + '(this.request)');
			} else {
				throw new Error('Invalid Action!');
			}
		} else {
			controller.view.load(page + '/index.tpl');
			controller.run(this.request);
		}
		controller.end();
    } else if (BARAKUS.io.fileExists(WEB_PATH + '/views/' + page + '/' + strtolower(action) + '.tpl')) {
        this.displayView(page + '/' + strtolower(action) + '.tpl');
    } else if (BARAKUS.io.fileExists(WEB_PATH + '/views/' + page + '/index.tpl')) {
        this.displayView(page + '/index.tpl');
    }
  },
  displayView: function(viewPath)
  {
    var view = new Barakus_Core_View();
    view.load(viewPath);
    view.setController(new Barakus_Core_Controller());
    view.assign('URL_BASE', '');
    view.show();        
  }
});